var cityMixin = {
    data(){
        return {
            cityId: '',
            cityList: [],
            campusList:[],
            campusId: '',
            classCache: {},
            cityCampusClass: {
                campusId: '',
                campusName: '',
                cityId:'',
                cityName: '',
                classId: '',
                className: ''
            },//校园信息
            classId: '',
            classList: [],
        }
    },
    methods: {
        loadCityWithType(type){
            var url = window.baseURL+`/backstage/${type}/findByPage?token=${this.$token}`;
            var cityPromise = this.$http.get(url)
            .then((response)=>{
                this.cityList = response.data.data.list.results;
                return response;
            })
            .catch((error)=>{
            })
            return cityPromise;
        },
        loadSingleCampusById(campusId){
            var url = window.baseURL + '/backstage/campus/find?token='+this.$token+"&id="+campusId;
            return this.$http.get(url)
            .then((response)=>{
                return response;
            })
        },
        //根据城市ID,获取该城市下的所有校区
        loadAllCampusByCityId(cityId){
             if(!cityId.length){
                 this.$alert('请先选择城市', '提示', {
                    confirmButtonText: '确定',
                    callback: action => {
                        this.$message({
                        type: 'info',
                        message: `action: ${ action }`
                        });
                    }
                    });
                return;
            }
            var campusURL = window.baseURL + "/backstage/campus/findByCity?token="+this.$token+"&cityId="+cityId;
           return this.$http.get(campusURL)
            .then((response)=>{
                this.campusList = response.data.data;
                return response.data.data;
            })
            .catch((error)=>{
            })
        },
        //根据校园ID获取该校园下的所有班级
        loadAllClassByCampusId(){
        //     if(!this.cityId.length){
        //         this.$alert('请先选择城市', '提示', {
        //            confirmButtonText: '确定',
        //            callback: action => {
        //                this.$message({
        //                type: 'info',
        //                message: `action: ${ action }`
        //                });
        //            }
        //            });
        //        return;
        //    }
        //     if(!this.campusId.length){
        //         this.$alert('请先选择校区', '提示', {
        //            confirmButtonText: '确定',
        //            callback: action => {
        //                this.$message({
        //                type: 'info',
        //                message: `action: ${ action }`
        //                });
        //            }
        //            });
        //        return;
        //    }
            var classURL = window.baseURL + "/backstage/classManage/findAllClass?token="+this.$token+"&campusId="+this.campusId;
            return this.$http.get(classURL)
            .then((response)=>{
                this.classList = response.data.data;
                return response.data.data
            })
        },
        //根据班级ID转换班级名称
        convertClassName(classId){
            var className = ''
            if(this.classCache[classId]){
                return this.classCache[classId];
            }else{
                this.classList.forEach((item)=>{
                    if(item.id == classId){
                         this.classCache[classId]=item.className;
                        return;
                    }
                })
            }
            return this.classCache[classId];
        }
    },
    created() {
        var account = JSON.parse(localStorage.getItem('responseData')).account;
        this.campusId = account.campusId;
    }
}

export default cityMixin;